#include "debug_show_recordlist.h"
#include "ui_debug_show_recordlist.h"
#include "bookstore.h"
#include "utils.h"
#include <vector>
#include <time.h>

using namespace utils;

debug_show_recordlist::debug_show_recordlist(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::debug_show_recordlist) {
    ui->setupUi(this);
}

debug_show_recordlist::~debug_show_recordlist() {
    delete ui;
}

void debug_show_recordlist::on_pushButton_clicked() {
    loadBookList();
}

void debug_show_recordlist::loadBookList() {
    auto size = sizeof (Record);//学生类的大小
    FILE *file = openfile(recordDatPath, size); //打开文件

    fseek(file, 0, SEEK_END);//fseek将文件指针指到文件末尾
    auto offset = ftell(file);//ftell得到文件从指针到文件头的大小（offset是文件总大小）

    int row = offset / size; //采取和增加用户时算ID大小一样的方法算数据行数
    ui->tableWidget->setColumnCount(8); //设置列数
    QStringList header;
    header << "ID" << "userID" << "bookID" << "borrowTime" << "latestReturnTime" << "returnTime" << "status" << "isReborrow";
    ui->tableWidget->setHorizontalHeaderLabels(header);
    ui->tableWidget->setRowCount(row);    //设置行数



    for(int i = 0; i < row; i++) {
        Record* tempRecord = bookstore::createRecord();

        fseek(file, size * i, SEEK_SET);   //将指针移到该放的地方
        fread(tempRecord, size, 1, file);          //读出需要查询的同学数据

        QString ID = QString::number(tempRecord->ID);
        QString userId = QString::number(tempRecord->userId);
        QString bookId = QString::number(tempRecord->bookId);
//        QString borrowTime = QString(QString::fromLocal8Bit(ctime(&tempRecord->borrowTime), 24));
//        QString latestReturnTime = QString(QString::fromLocal8Bit(ctime(&tempRecord->latestReturnTime), 24));
//        QString returnTime = QString(QString::fromLocal8Bit(ctime(&tempRecord->returnTime), 24));
        QString borrowTime = fomatTime(tempRecord->borrowTime);
        QString latestReturnTime = fomatTime(tempRecord->latestReturnTime);
        QString returnTime = fomatTime(tempRecord->returnTime);
        QString status = QString::number(tempRecord->status);
        QString isReborrwed = QString::number(tempRecord->isReborrwed);

        ui->tableWidget->setItem(i, 0, new QTableWidgetItem(ID));
        ui->tableWidget->setItem(i, 1, new QTableWidgetItem(userId));
        ui->tableWidget->setItem(i, 2, new QTableWidgetItem(bookId));
        ui->tableWidget->setItem(i, 3, new QTableWidgetItem(borrowTime));
        ui->tableWidget->setItem(i, 4, new QTableWidgetItem(latestReturnTime));
        ui->tableWidget->setItem(i, 5, new QTableWidgetItem(returnTime));
        ui->tableWidget->setItem(i, 6, new QTableWidgetItem(status));
        ui->tableWidget->setItem(i, 7, new QTableWidgetItem(isReborrwed));

        delete(tempRecord);
    }
    fclose(file);//关闭文件
}
